﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace BattlelogMobile.Client.Converter
{
    /// <summary>
    /// Convert image name to correct url
    /// </summary>
    public class ImageUrlConverter : IValueConverter
    {
        private const string Rank = "Rank";
        private const string RankImageUrl = "http://battlelog-cdn.battlefield.com/public/profile/bf3/stats/ranks/small/r{0}.png?v=3173239";
        private const string Kit = "Kit";
        private const string KitImageUrl = "http://battlelog-cdn.battlefield.com/public/profile/bf3/kit-icon-{0}.png?v=3173239";
        private const string Weapon = "Weapon";
        private const string WeaponImageUrl = "http://battlelog-cdn.battlefield.com/public/profile/bf3/stats/items_90x54/{0}.png?v=3173239";
        private const string Vehicle = "Vehicle";
        private const string Gadget = "Gadget";
        private const string VehicleAndGadgetImageUrl = "http://battlelog-cdn.battlefield.com/public/profile/bf3/stats/items_147x88/{0}.png?v=3173239";
        private const string Award = "Award";
        private const string RibbonAwardPrefix = "r";
        private const string MedalAwardPrefix = "m";
        private const string RibbonAwardImageUrl = "http://battlelog-cdn.battlefield.com/public/profile/bf3/stats/ribbons/s/{0}.png?v=3173239";
        private const string MedalAwardImageUrl = "http://battlelog-cdn.battlefield.com/public/profile/bf3/stats/medals/s/{0}.png?v=3173239";
        private const string Gravatar = "Gravatar";
        private const string GravatarImageUrl = "http://www.gravatar.com/avatar/{0}";
        private const string DefaultGravatarImageUrl = "http://battlelog-cdn.battlefield.com/public/base/shared/default-avatar-52.png?v=7909";
        private const string DefaultGravatarMd5 = "d41d8cd98f00b204e9800998ecf8427e";

        /// <summary>
        /// Return value is complete url,
        /// converter parameter is mandatory for correct functionality 
        /// </summary>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string imageUrl = string.Empty;
            if (parameter != null)
            {
                string imageType = parameter.ToString();

                // Soldier rank
                if (imageType.Equals(Rank))
                    imageUrl = string.Format(RankImageUrl, value);
                // Small kit image (assault, engineer, support, rewcon)
                else if (imageType.Equals(Kit))
                    imageUrl = string.Format(KitImageUrl, value.ToString().ToLowerInvariant());
                // Weapon 
                else if (imageType.Equals(Weapon))
                    imageUrl = string.Format(WeaponImageUrl, value);
                // Vehicle
                else if (imageType.Equals(Vehicle) || imageType.Equals(Gadget))
                    imageUrl = string.Format(VehicleAndGadgetImageUrl, value);
                // Gravatar, default battlelog image is returned if user has not st gravatar account
                else if (imageType.Equals(Gravatar) && value.ToString().Equals(DefaultGravatarMd5))
                    imageUrl = DefaultGravatarImageUrl;
                else if (imageType.Equals(Gravatar))
                    imageUrl = string.Format(GravatarImageUrl, value);
                // Ribbon
                else if (imageType.Equals(Award) && value.ToString().StartsWith(RibbonAwardPrefix))
                    imageUrl = string.Format(RibbonAwardImageUrl, value);
                // Medal
                else if (imageType.Equals(Award) && value.ToString().StartsWith(MedalAwardPrefix))
                    imageUrl = string.Format(MedalAwardImageUrl, value);
            }

            return imageUrl;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
